﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        private static DataTable GetData = new DataTable();
        private DataTable Data
        {
            get
            {
                if (GetData.Rows.Count > 0)
                {
                    return GetData;
                }
                else
                {
                    var sql = "select * from Articles";
                    var dt = DbHelper.GetData(sql);

                    gv1.DataSource = dt;
                    gv1.DataBind();
                    return dt;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }

        }

        private void FillData()
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetData(sql);

            gv1.DataSource = dt;
            gv1.DataBind();
        }

        //取消
        protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv1.EditIndex = -1;
            FillData();
        }
        //删除
        protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var sql = string.Format("delete from Articles where id ={0}", id);
            DbHelper.AddOrUpdate(sql);

            gv1.EditIndex = -1;
            FillData();
        }
        //编辑
        protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv1.EditIndex = e.NewEditIndex;
            FillData();
        }
        //更新
        protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetValue(e.RowIndex, 0);
            var isOk = int.TryParse(idString, out int id);

            var Title = GetValue(e.RowIndex, 1);
            var Content = GetValue(e.RowIndex, 2);
            var Author = GetValue(e.RowIndex, 3);
            var Remarks = GetValue(e.RowIndex, 4);

            if (isOk)
            {
                var sql = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id='{4}'", Title, Content, Author, Remarks, id);
                DbHelper.AddOrUpdate(sql);
            }
            else
            {
                var sql2 = string.Format("insert into Articles(Title,Content,Author,Remarks) values('{0}','{1}','{2}','{3}')", Title, Content, Author, Remarks);
                DbHelper.AddOrUpdate(sql2);
            }

            gv1.EditIndex = -1;
            FillData();
        }

        private string GetValue(int rowIndex, int colIndex)
        {
            var cell = gv1.Rows[rowIndex].Cells[colIndex];

            var v = cell.Controls.Count > 0 ? ((TextBox)cell.Controls[0]).Text : cell.Text;
            return v;
        }

        //添加

        protected void Unnamed1_Click1(object sender, EventArgs e)
        {
            var dt = this.Data.Copy();
            dt.Rows.Add(dt.NewRow());
            GetData = dt;

            gv1.DataSource = GetData;
            gv1.DataBind();

            gv1.EditIndex = dt.Rows.Count - 1;




            gv1.DataSource = GetData;
            gv1.DataBind();
        }
    }
}